#include <cstdio>
#include <cstring>

struct tree_node {
    tree_node *left_child;
    tree_node *right_child;
    int data;
} binary_tree[110];

int assigned_num;

tree_node *creat_node() {
    binary_tree[assigned_num].left_child = binary_tree[assigned_num].right_child = nullptr;
    return &binary_tree[assigned_num++];
}

void post_order(tree_node *node) {
    if (node->left_child != nullptr)
        post_order(node->left_child);
    if (node->right_child != nullptr)
        post_order(node->right_child);
    printf("%d ", node->data);
}

void mid_order(tree_node *node) {
    if (node->left_child != nullptr)
        mid_order(node->left_child);
    printf("%d ", node->data);
    if (node->right_child != nullptr)
        mid_order(node->right_child);
}

void pre_order(tree_node *node) {
    printf("%d ", node->data);
    if (node->left_child != nullptr)
        pre_order(node->left_child);
    if (node->right_child != nullptr)
        pre_order(node->right_child);
}

tree_node *insert_node(tree_node *node, int num) {
    if (node == nullptr) {
        node = creat_node();
        node->data = num;
    } else if (num < node->data) {
        node->left_child = insert_node(node->left_child, num);
    } else if (num > node->data) {
        node->right_child = insert_node(node->right_child, num);
    }
    return node;
}

int main(int argc, char const *argv[]) {
    int n;
    while (scanf("%d", &n) != EOF) {
        assigned_num = 0;
        tree_node *root_node = nullptr;
        for (int i = 0; i < n; ++i) {
            int num;
            scanf("%d", &num);
            root_node = insert_node(root_node, num);
        }
        pre_order(root_node);
        printf("\n");
        mid_order(root_node);
        printf("\n");
        post_order(root_node);
        printf("\n");
    }
    return 0;
}